<template>
  <div id="login">
    <el-row>
      <el-col :span="14"><div class="bg"></div></el-col>
      <el-col :span="10" class="loginForm">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
          label-position="right"
          @submit.native.prevent
        >
          <div class="title">
            <span>用户注册</span>
            <router-link to="/login" class="login">登录</router-link>
          </div>
          <el-form-item label="用户名" prop="account">
            <el-input
              v-model="ruleForm.acccount"
              placeholder="用户名/手机/邮箱"
            >
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"
              ><i slot="suffix-icon" class="el-input__icon el-icon-view"></i>
            </el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm('ruleForm')"
              native-type="submit"
              >提交</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import {request} from "../../utils/index.js";
export default {
  data() {
    let checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        }
      }, 1000);
    };
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        acccount: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.register();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    register() {
      request({
        method: "post",
        url: this.portInterface.registerUrl,
        data: {
          account: this.ruleForm.acccount,
          password: this.$getRsaCode(this.ruleForm.pass),
        },
      }).then(() => {
        this.$message({
          showClose: true,
          message: "注册成功,正在跳转到登录页...",
          type: "success",
        });
        setTimeout(() => {
          this.$router.push({
            name: "login",
          });
        }, 1000);
      });
    },
  },
};
</script>
<style lang="less" scoped>
#login {
  height: 100%;
  .el-row {
    height: 100%;
    .el-col {
      height: 100%;
    }
  }

  .bg {
    height: 100%;
    background: url("../../assets/images/map.png") center center no-repeat;
  }
}
.loginForm {
  display: flex;
  justify-content: center;
  align-items: center;
  .el-form {
    position: relative;
    padding: 10px 20px;
    height: 50%;
    min-height: 350px;
    background: #13227a;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}
/deep/.el-form-item__label {
  color: #ffffff;
  width: 70px !important;
}
/deep/.el-form-item__content {
  margin-left: 70px !important;
}
.title {
  color: #ffffff;
  display: flex;
  justify-content: center;
  .login {
    color: #409eff;
    text-decoration: underline;
    position: absolute;
    right: 10px;
    top: 10px;
  }
  margin-bottom: 22px;
}
.title > span,
.register {
  font-size: 18px;
}
#login > .el-row {
  height: 100%;
  margin: 0;
  padding: 0;
  .el-col {
    height: 100%;
  }
}
</style>